import { createActivity,getTenantActivityInfo,updateActivity } from '@/util/api/activity.js'
export default {
  data() {
    return {
      type: '1', // 1新增 2编辑
      id: '',
      barTitle: '创建活动',
      activityInfo: {},
      companyNameList: [], // 搜索结果
      activityId: '',
      form: {
        beginDate: '',
        endDate: '',
        title: '',
        synopsis: '',
      },
      tenantInfo: {},
      showStart: false,
      showEnd: false,

      currentDate: new Date(),
      currentDate2: new Date(),

      time1: 0,
      time2: 0,
      value1: Number(new Date()),
      value2: Number(new Date()),
    }
  },
  onLoad(e) {
    if(e.type) {
      this.type = e.type
      this.id = e.id
      this.barTitle = '修改活动'
      this.getDetail()
    }
    uni.setNavigationBarTitle({
      title: this.barTitle, 
    })
  },
  methods: {
    getDetail() {
      getTenantActivityInfo({id: this.id}).then(res=>{        
        if(res.code === 200) { 
          this.form = res.result
        }else{
          uni.showToast({
            title: res.message,
            duration: 2000,
            icon: "none"
          });
        }
      })
    },
    onConfirmTimes(v) {
      this.form.beginDate = this.timeFormat(new Date(v));
      this.cancel();
    },
    onConfirmTimes2(v) {
      this.form.endDate = this.timeFormat(new Date(v));
      this.cancel();
    },
    cancel() {
      this.showStart = false;
      this.showEnd = false;
    },
    timeFormat(time) {
      // 时间格式化 2019-09-08
      let year = time.getFullYear();
      let month = (time.getMonth() + 1).toString().padStart(2, "0");
      let day = time
        .getDate()
        .toString()
        .padStart(2, "0");
      let HH = time.getHours() + ':';
      let mm = time.getMinutes();
      return year + "-" + month + "-" + day + ' ' + HH + mm;
    },
    onStartTime(e) {
      this.time1 = e.value
      if(this.time1 > this.time2 && this.time2){
        uni.showToast({
          title: '开始时间不能大于结束时间',
          duration: 2000,
          icon: "none"
        });
        return
      }
      const timeFormat = uni.$u.timeFormat;
      this.form.beginDate = timeFormat(e.value, 'yyyy-mm-dd hh:MM');
      this.showStart = false
    },
    onStartClose() {
      this.showStart = false
    },
    onEndTime(e) {
      this.time2 = e.value
      if(this.time2 < this.time1){
        uni.showToast({
          title: '结束时间不能小于开始时间',
          duration: 2000,
          icon: "none"
        });
        return
      }
      const timeFormat = uni.$u.timeFormat;
      this.form.endDate = timeFormat(e.value, 'yyyy-mm-dd hh:MM');
      this.showEnd = false
    },
    onEndClose() {
      this.showEnd = false
    },
    submitForm() {
      if(this.type==='2'){
        this.editActive()
      }else{
        this.addActive()
      }
    },
    addActive() {
      let arr = [
        { value: this.form.title, text: "请输入活动标题"},
        { value: this.form.synopsis, text: "请输入活动简介"},
        { value: this.form.beginDate, text: "请选择活动开始时间"},
        { value: this.form.endDate, text: "请选择活动结束时间"},
      ]
      for (let i = 0; i < arr.length; i++) {
        let obj = arr[i];
        if (obj.value === "") {
          uni.showToast({
            title: obj.text,
            duration: 2000,
            icon: "none"
          });
          return;
        }
      }
      createActivity(this.form).then(res=>{
        if(res.code == 200){
          setTimeout(()=>{
            uni.showToast({
              title: '提交成功！',
              duration: 2000,
              icon: "none"
            });
          },100)
          setTimeout(() => {
            if(this.type === 3){
              uni.redirectTo({
                url: '/pages/activity/createList/Index',
              })
            }else{
              uni.navigateBack()
            }
          }, 2000)
        }else{
          setTimeout(()=>{
            uni.showToast({
              title: res.message,
              duration: 2000,
              icon: "none"
            });
          },100)
        }
      })
    },
    editActive() {
      let arr = [
        { value: this.form.title, text: "请输入活动标题"},
        { value: this.form.synopsis, text: "请输入活动简介"},
      ]
      for (let i = 0; i < arr.length; i++) {
        let obj = arr[i];
        if (obj.value === "") {
          uni.showToast({
            title: obj.text,
            duration: 2000,
            icon: "none"
          });
          return;
        }
      }
      let params = Object.assign(this.form,{id:this.id})
      updateActivity(params).then(res=>{
        if(res.code == 200){
          setTimeout(()=>{
            uni.showToast({
              title: '提交成功！',
              duration: 2000,
              icon: "none"
            });
          },100)
          setTimeout(() => {
            uni.navigateBack()
          }, 2000)
        }else{
          setTimeout(()=>{
            uni.showToast({
              title: res.message,
              duration: 2000,
              icon: "none"
            });
          },100)
        }
      })
    },
  }
}